<template>
    <el-dialog title="密码修改" :visible.sync="dialogVisible" width="500px" :before-close="handleClose" :modal-append-to-body="false">
      <el-form :model="form" :rules="rules" ref="ruleForm" onSubmit="return false;" v-loading="passwordLoading">
        <el-form-item label=当前密码 prop="opassword" required>
          <el-input v-model="form.opassword" :show-password="true"></el-input>
        </el-form-item>
        <el-form-item label="新密码" prop="password" required>
          <el-input v-model="form.password" :show-password="true"></el-input>
        </el-form-item>
        <el-form-item label="再次新密码" prop="repassword" required>
          <el-input v-model="form.repassword" :show-password="true"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false" size="mini">取 消</el-button>
        <el-button type="primary" @click="doUpdatePassword('ruleForm')" size="mini">确 定</el-button>
      </span>
    </el-dialog>
</template>

<script>
import { mapState } from 'vuex'
export default {
  computed: {
    ...mapState([
      'user',
      'loginUrl'
    ])
  },
  data () {
    var validatePass = (rule, value, callback) => {
      let reg = /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)[^]{8,32}$/
      if (value === '') {
        callback(new Error('请输入新密码'))
      } else if (!reg.test(value) || value.length < 8 || value.length > 32) {
        callback(new Error('密码为8-32位，必须由大、小写字母和数字组成！'))
      } else {
        callback()
      }
    }
    var validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入新密码'))
      } else if (value !== this.form.password) {
        callback(new Error('两次输入密码不一致!'))
      } else {
        callback()
      }
    }

    return {
      dialogVisible: false,
      passwordLoading: false,
      form: {
        opassword: '',
        password: '',
        repassword: ''
      },
      rules: {
        opassword: [
          { required: true, message: '请输入当前密码', trigger: 'blur' }
        ],
        password: [
          { validator: validatePass, trigger: 'blur' },
          { min: 8, max: 32, message: '长度在 8 到 32 个字符', trigger: 'blur' }
        ],
        repassword: [
          { validator: validatePass2, trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    show () {
      this.dialogVisible = true
      this.form = {
        opassword: '',
        password: '',
        repassword: ''
      }
      this.$nextTick(() => {
        this.$refs['ruleForm'].clearValidate()
      })
    },
    // 执行修改密码
    doUpdatePassword (formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.passwordLoading = true
          this.$post('/user/resetpassword', {
            opassword: this.form.opassword,
            password: this.form.password
          }).then(response => {
            if (parseInt(response) === 1) {
              this.dialogVisible = false
            }
            this.passwordLoading = false
          })
        } else {
          return false
        }
      })
    },
    // 修改密码弹窗关闭后
    handleClose () {
      this.dialogVisible = false
    }
  }
}

</script>

<style scoped>
.user {
  margin: 0 8px;
}
.g-dropdown {
  cursor: pointer;
}
</style>
